#include <stdio.h>

#define MaxSize 100
typedef int DataType;

typedef struct
{
	DataType  list[MaxSize];
	int size;
}SeqList;

void ListInitiate(SeqList *L)
{
	L->size = 0;
}

int ListLength(SeqList L)
{
	return L.size;
}

int ListInsert(SeqList *L,int i,DataType x)
{
	int j;
	if(L->size >= MaxSize)
	{
		printf("顺序表已满无法插入");
		return 0;
	}
	else if(i < 0 ||i > L->size)
	{
		printf("参数i不合法！\n");
		return 0;
	}
	else
	{ //此段程序有错误
		for(j = L->size-1; j >= i-1; j--)
			L->list[j+1] = L->list[j];
		L->list[i] = x;
		L->size ++;
		return 1;
	}
}

int ListDelete(SeqList *L,int i,DataType *x)
{
	int j;
	if(L->size <= 0)
	{
		printf("顺序表已空无数据元素可删！ \n");
		return 0;
	}
	else if(i < 0 || i > L->size-1)
	{
		printf("参数i不合法");
		return 0;
	}
	else{//此段程序有错误
		*x = L->list[i];
		for(j = i ;j <= L->size-1; j++)L->list[j] = L->list[j+1];
		L->size--;
		return 1;
	}
}

int ListGet(SeqList L, int i, DataType *x)
{
	if(i < 0 || i > L.size-1)
	{
		printf("参数i不合法！\n");
		return 0;
	}
	else
	{
		*x = L.list[i];
		return 1;
	}
}

int ListSearch(SeqList L,DataType x){
	int low = 0, high = L.size, mid,y;
	while(low <= high){
		mid = (low + high)/2;	//取中间位置
		if(L.list[mid] == x){
			y = L.list[mid];
			printf("\n搜索结果为:%d",y);
			return 1;
		}else if(L.list[mid] > x){
			high = mid - 1;	//从前半部分继续查找
		}else{
			low = mid + 1;	//从后半部分继续查找
		}
	}
	printf("\n超出搜索范围\n");
	return -1;	//查找失败，返回-1
}

int main(int argc,char * argv[])
{
	SeqList myList;
	int i , x , y;
	ListInitiate(&myList);
	for(i = 0; i < 10;i++)
		ListInsert(&myList, i, i+1);
	ListDelete(&myList, 4, &x);
	for(i=0; i < ListLength(myList);i++)
	{//此段程序有一处错误
		ListGet(myList,i,&x);
		printf("%d",x);
	}
	ListSearch(myList,9);
	ListSearch(myList,11);
	return 0;
}

